
package com.mt.mall.controller.skill;

import com.mt.common.core.annotation.RequestRateLimiter;
import com.mt.common.core.web.JsonResult;
import com.mt.mall.entity.Order.Order;
import org.apache.ibatis.annotations.Param;
import org.redisson.api.RateIntervalUnit;
import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mt.mall.dto.skill.SkillCommodityEditDto;
import com.mt.mall.entity.skill.SkillCommodity;
import com.mt.mall.service.skill.SkillCommodityService;
import com.mt.mall.service.good.CommodityService;
import com.mt.mall.service.good.SpecificationStockService;

@Api(tags = "秒杀商品")
@RestController
@RequestMapping("/api/mall/skill/SkillCommodity")
@CrossOrigin(allowCredentials = "true")
public class SkillCommodityController {
    private static Logger logger = LoggerFactory.getLogger(SkillCommodityController.class);

    @Autowired
    private SkillCommodityService skillCommodityService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private SpecificationStockService specificationStockService;

    /**
     * 根据分页参数查询秒杀商品集合
     *
     * @param pageDTO
     *            分页条件
     */
    // @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:view')")
    @ApiOperation("分页查询秒杀商品")
    @ApiPageParam
    @PostMapping("/findSkillCommoditys")
    public PageResultDTO findSkillCommoditys(@RequestBody PageDTO pageDTO) {
        return this.skillCommodityService.findSkillCommoditys(pageDTO);
    }

    /**
     * 根据ID查询指定的秒杀商品
     *
     * @param skillCommodityId
     *            Id
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:view')")
    @ApiOperation("根据id查询秒杀商品")
    @ApiPageParam
    @PostMapping("/findSkillCommodity")
    public SkillCommodity findSkillCommodity(@RequestParam Long skillCommodityId) {
        return this.skillCommodityService.findSkillCommodity(skillCommodityId);
    }

    /**
     * 根据ID查询指定的秒杀商品(包含外键名称)
     *
     * @param skillCommodityId
     *            Id
     */
    // @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:view')")
    @ApiOperation("根据ID查询指定的秒杀商品(包含外键名称)")
    @PostMapping("/findSkillCommodityForView")
    public SkillCommodity findSkillCommodityForView(@RequestParam Long skillCommodityId) {
        return this.skillCommodityService.findSkillCommodityWithForeignName(skillCommodityId);
    }

    /**
     * 根据ID查询指定的秒杀商品(包含秒杀商品和外键名称)
     *
     * @param skillCommodityId
     *            Id
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:view')")
    @ApiOperation("根据ID查询指定的秒杀商品(包含秒杀商品和外键名称)")
    @PostMapping("/findSkillCommodityForEdit")
    public SkillCommodityEditDto findSkillCommodityForEdit(@Param("skillCommodityId") Long skillCommodityId) {
        SkillCommodityEditDto skillCommodityEditDto = new SkillCommodityEditDto();
        skillCommodityEditDto
            .setSkillCommodity(this.skillCommodityService.findSkillCommodityWithForeignName(skillCommodityId));

        this.prepareSkillCommodityEditDto(skillCommodityEditDto);

        return skillCommodityEditDto;
    }

    /**
     * 根据ID查询指定的秒杀商品(只提取ID 和 Name)
     *
     * @param skillCommodityId
     *            Id
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:view')")
    @ApiOperation("根据ID查询指定的秒杀商品(只提取ID 和 Name)")
    @PostMapping("/findSkillCommoditysWithIdNameById")
    public SkillCommodity findSkillCommoditysWithIdNameById(@RequestParam Long skillCommodityId) {
        return this.skillCommodityService.findSkillCommoditysWithIdNameById(skillCommodityId);
    }

    /**
     * 根据名称查询秒杀商品集合(只提取ID 和 Name)
     *
     * @param skillCommodityName
     *            名称
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:view')")
    @ApiOperation("根据名称查询秒杀商品集合(只提取ID 和 Name)")
    @PostMapping("/findSkillCommoditysWithIdNameByName")
    public List<SkillCommodity> findSkillCommoditysWithIdNameByName(@RequestParam String skillCommodityName) {
        return this.skillCommodityService.findSkillCommoditysWithIdNameByName(skillCommodityName);
    }

    /**
     * 创建新的秒杀商品
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:add')")
    @ApiOperation("创建新的秒杀商品")
    @PostMapping("/createSkillCommodity")
    public SkillCommodityEditDto createSkillCommodity() {
        SkillCommodityEditDto skillCommodityEditDto = new SkillCommodityEditDto();
        skillCommodityEditDto.setSkillCommodity(new SkillCommodity());

        this.prepareSkillCommodityEditDto(skillCommodityEditDto);
        return skillCommodityEditDto;
    }

    private void prepareSkillCommodityEditDto(SkillCommodityEditDto skillCommodityEditDto) {
        // TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        skillCommodityEditDto.setCommodityCommoditys(this.commodityService.findAllCommoditysWithIdName());
        // TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        skillCommodityEditDto.setSpecificationsSpecificationStocks(
            this.specificationStockService.findAllSpecificationStocksWithIdName());
    }

    /**
     * 新增保存秒杀商品
     *
     * @param skillCommodity
     *            实体对象
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:add')")
    @ApiOperation("新增保存秒杀商品")
    @PostMapping("/saveSkillCommodity")
    public SkillCommodity saveSkillCommodity(@RequestBody SkillCommodity skillCommodity) {
        return this.skillCommodityService.saveSkillCommodity(skillCommodity);
    }

    /**
     * 修改保存秒杀商品
     *
     * @param skillCommodity
     *            实体对象
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:update')")
    @ApiOperation("修改保存秒杀商品")
    @PostMapping("/updateSkillCommodity")
    public SkillCommodity updateSkillCommodity(@RequestBody SkillCommodity skillCommodity) {
        return this.skillCommodityService.updateSkillCommodity(skillCommodity);
    }

    /**
     * 根据ID删除秒杀商品
     *
     * @param skillCommodityId
     *            ID
     */
    @PreAuthorize("hasAuthority('mall:skill:SkillCommodity:remove')")
    @ApiOperation("根据ID删除秒杀商品")
    @PostMapping("/deleteSkillCommodity")
    public void deleteSkillCommodity(@RequestParam Long skillCommodityId) {
        this.skillCommodityService.deleteSkillCommodity(skillCommodityId);
    }

    @RequestRateLimiter(key = "putSkill", rate = 1, rateInterval = 10, timeUnit = RateIntervalUnit.MILLISECONDS)
    @ApiOperation("提交秒杀商品")
    @PostMapping("/putSkill")
    public JsonResult putSkill(@Param("skillCommodityId") Long skillCommodityId) {
        return this.skillCommodityService.putSkill(skillCommodityId);
    }

    @RequestRateLimiter(key = "skillOrder", rate = 1, rateInterval = 100, timeUnit = RateIntervalUnit.MILLISECONDS)
    @ApiOperation("秒杀商品")
    @PostMapping("/skillOrder")
    public JsonResult skillOrder(@RequestBody Order order) {
        return this.skillCommodityService.skillOrder(order);
    }

}
